// JavaScript Document

// Input Box

var ErrorColor = "#E0F2C0";
var nonErrorColor = "#FFF";

function validateInput(fieldID,fieldName)
{
	if(fieldID.value=="")		
	{
		Error += "Error: "+fieldName+" is empty.\n"; 
		fieldID.style.background = ErrorColor;
		Valid = false;		
	}
	else
	{
		fieldID.style.background = nonErrorColor;
	}
}


// Email

function validateEmail(fieldID)
{
	var fld = fieldID;
	var tfld = (fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
		fld.style.background = ErrorColor;
        Error += "Error: Email address is empty.\n";
		Valid = false;
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = ErrorColor;
        Error += "Error: Please enter a valid email address.\n";
		Valid = false;
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = ErrorColor;
        Error += "Error: The email address contains illegal characters.\n";
		Valid = false;
    } else {
        fld.style.background = nonErrorColor;
    }
}


// Phone / Mobile

function validatePhone(fieldID,fieldName) 
{
	var fld = fieldID;
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
   if (fld.value == "") {
        Error += "Error: "+fieldName+" number is empty.\n";
        fld.style.background = ErrorColor;
		Valid = false;
    } else if (isNaN(parseInt(stripped))) {
        Error += "Error: "+fieldName+" number contains illegal characters.\n";
        fld.style.background = ErrorColor;
		Valid = false;
    } else if (!(stripped.length >= 10)) {
        Error += "Error: "+fieldName+" number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = ErrorColor;
		Valid = false;
    }
	else
	{
		fld.style.background = nonErrorColor;
	}
}


// Select Box

function validateSelect(fieldID,fieldName)
{
	if(fieldID.value=="")		
	{
		Error += "Error: Please select "+fieldName+".\n"; 
		fieldID.style.background = ErrorColor;
		Valid = false;		
	}
	else
	{
		fieldID.style.background = nonErrorColor;
	}
}

// Password Or Equal

function validatePasswordOrEqual(pass,rpass)
{
	if(pass.value != rpass.value)
	{
		Error += "Error: Password and confirm password does not match.\n"; 
		pass.style.background = ErrorColor;
		rpass.style.background = ErrorColor;
		Valid = false;		
	}
	else if(pass.value != "")
	{
		pass.style.background = nonErrorColor;
		rpass.style.background = nonErrorColor;
	}
}

// IS Numeric

function IsNumeric(fieldID,fieldName)
{
	var strValidChars = "0123456789";
	var strChar;
	var cnt = 0;
	if (fieldID.value.length == 0) 
	{ 
		Error += "Error: "+fieldName+" is empty.\n"; 
		fieldID.style.background = ErrorColor;
		Valid = false;
	}
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < fieldID.value.length; i++)
	{
	  strChar = fieldID.value.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1)
		 {
			cnt=cnt+1;
		 }
	}
	if(cnt >0)
	{
		Error += "Error: "+fieldName+" contains illegal characters.\n";
		fieldID.style.background = ErrorColor;
		Valid = false;
	}	
	if(cnt ==0 && fieldID.value.length > 0)
	{
		fieldID.style.background = nonErrorColor;
	}
}

function IsCharacter(fieldID,fieldName)
{
	var strValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	var strChar;
	var cnt = 0;
	if (fieldID.value.length == 0) 
	{ 
		Error += "Error: "+fieldName+" is empty.\n"; 
		fieldID.style.background = ErrorColor;
		Valid = false;
	}
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < fieldID.value.length; i++)
	{
	  strChar = fieldID.value.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1)
		 {
			cnt=cnt+1;
		 }
	}
	if(cnt >0)
	{
		Error += "Error: Numeric and special characters are not allowed in "+fieldName+".\n";
		fieldID.style.background = ErrorColor;
		Valid = false;
	}	
	if(cnt ==0 && fieldID.value.length > 0)
	{
		fieldID.style.background = nonErrorColor;
	}
}


